const dataBaseMethods = require('../db')
const tool = require('../tools/tool')

module.exports = {
    parseValue(type, value) {
        if (type == 'string') {
            return value.toString()
        }
        if (type == 'number') {
            return parseFloat(value)
        }
        if (type == 'date') {
            return new Date(value)
        }
        if (type == 'object') {
            if (typeof (value) == 'object') return value
            return JSON.parse(value)
        }
        if (type == 'boolean') {
            return value.toString() == 'true' ? true : false
        }
        return value
    },
    async getItem(key) {
        let re = await dataBaseMethods.select("select * from settings where `key` = '" + key.toString() + "'")
        if (re.length) {
            return this.parseValue(tool.JSON.parse(re[0].value).type, tool.JSON.parse(re[0].value).value)
        } else {
            return null
        }
    },
    async getItemInfo(key) {
        let re = await dataBaseMethods.select("select * from settings where `key` = '" + key.toString() + "'")
        if (re.length) {
            let t = re[0]
            t.valueParsed = this.parseValue(tool.JSON.parse(re[0].value).type, tool.JSON.parse(re[0].value).value)
            return t
        } else {
            return undefined
        }
    },
    async setItem(key, value, explain = undefined) {
        if (!key) {
            throw new Error(`未指定key`)
        }
        let t
        if ((await this.getItem(key)) != null) {
            t = await this.getItem(key)
            await this.removeItem(key)
        }
        await dataBaseMethods.insertInto('settings', {
            key: key,
            value: JSON.stringify({
                type: typeof (value),
                value: value
            }),
            explain: (explain == undefined) ? (t ? t.explain : '') : explain
        })
    },
    async removeItem(key) {
        await dataBaseMethods.exe(`delete from settings where \`key\` = '${key}'`)
    },
    list: [
        {
            key: 'reDoNotEssayQuestionNum',
            default: 50,
            explain: '重做行测题个数',
        },
        {
            key: 'reDoEssayQuestionNum',
            default: 1,
            explain: '重做申论题个数',
        },
        {
            key: 'passScore',
            default: 70,
            explain: '及格线',
        },
        {
            key: 'AIParseFinishedDocList',
            default: [],
            explain: 'AI解析已经完成的文件列表',
        },
    ],
    async init() {
        let currentList = await dataBaseMethods.select(`select * from settings`)
        for (let i = 0; i < currentList.length; i++) {
            currentList[i].valueParsed = this.parseValue(tool.JSON.parse(currentList[i].value).type, tool.JSON.parse(currentList[i].value).value)
        }
        const exist = (key) => {
            for (let i = 0; i < currentList.length; i++) {
                if (currentList[i].key == key) {
                    return true
                }
            }
            return false
        }
        for (let i = 0; i < this.list.length; i++) {
            if (exist(this.list[i].key) == false) {
                await this.setItem(this.list[i].key, this.list[i].default, this.list[i].explain)
            }
        }
    }
}